package com.homework.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.homework.mapper.UserMapper;
import com.homework.pojo.User;
import com.homework.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UserControll {
    @Autowired
    private UserService service;
    @RequestMapping("/adduser")
    public String adduser() {
        return "adduser";
    }

    @RequestMapping("/adduser1")

    public String adduser1(User user) throws Exception {
        if (user.getUser_name() == null) {
            throwex("用户名不能为空");
            return "defeat";

        }
        if (user.getPassword() == null) {
            throwex("密码不能为空");

            return "defeat";
        }
        if (user.getEmail() == null) {
            throwex("email不能为空");

            return "defeat";
        }
        if (user.getReal_name() == null) {
            throwex("真实姓名不能为空");

            return "defeat";
        }
        if (service.adduser(user) > 0) {
            return "success";
        } else {
            throwex("登录失败");
            return "defeat";
        }
    }

    public void throwex(String s) throws Exception {
        throw new Exception(s);
    }

    @RequestMapping("/delete")
    public String delete() {
        return "delete";
    }

    @RequestMapping("/delete1")
    public String delete1(String username, String password) throws Exception {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("user_name", username);
        qw.eq("password", password);
        if (service.delete(qw) > 0)
            return "success";
        else {
            throwex("没有该用户");
        }
        return "defeat";
    }

    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    @RequestMapping("/login1")
    public String login1(String username, String password) throws Exception {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("user_name", username);

        qw.eq("password", password);
        if (!service.login(qw))
            return "success";
        else {
            throwex("没有该用户");
        }
        return "defeat";
    }

    @RequestMapping("/updatename")
    public String updateusername() {
        return "updatename";
    }

    @RequestMapping("/updatename1")
    public String updateusername1(String oldusername, String newusername, String password) throws Exception {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("user_name", oldusername);
        qw.eq("password", password);
        User user = new User();
        user.setUser_name(newusername);
        System.err.println(user+"更改");
        if (service.update(user, qw) == 0) {
            throwex("没有该用户");
        }

        return"success";
    }


    @RequestMapping("/updatepassword")
    public String updatepassword() {
        return "updatepassword";
    }

    @RequestMapping("/updatepassword1")
    public String updatepassword1(String oldpassword, String newpassword, String username) throws Exception {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("user_name", username);
        qw.eq("password", oldpassword);
        User user = new User();
        user.setPassword(newpassword);
        if (service.update(user, qw) == 0) {
            throwex("没有该用户");

        }
        return "success";

    }
}
